<template>
  <div class="table-page">
    <GiTable
      row-key="userWechatGroupId"
      v-model:columns="columns"
      :loading="loading"
      :data="tableData"
      :scroll="{ x: '100%', y: '100%', minWidth: 1000 }"
      :pagination="pagination"
      @sorter-change="onSorterChange"
      @refresh="getTableData"
      :simple="true"
    >
      <template #custom-extra>
        <a-form
          label-align="right"
          layout="inline"
          auto-label-width
          :model="listParams"
          size="small"
          class="form my-10"
        >
          <a-form-item field="status" label="开启状态">
            <a-select
              v-model="listParams.isOpen"
              @change="changeCurrent(1)"
              placeholder="请选择"
              class="w-30n"
              allow-clear
            >
              <a-option value="">全部</a-option>
              <a-option :value="1">开启中</a-option>
              <a-option :value="0">未开启</a-option>
            </a-select>
          </a-form-item>
          <a-form-item>
            <a-space>
              <a-button type="primary" size="small" @click="changeCurrent(1)">
                <template #icon>
                  <icon-search />
                </template>
                <template #default>查询</template>
              </a-button>
              <a-button size="small" @click="onResetSearch">
                <template #icon>
                  <icon-sync />
                </template>
                <template #default>重置</template>
              </a-button>
            </a-space>
          </a-form-item>
        </a-form>
      </template>
      <template #greetContent="{ record }">
        <template v-if="record.greetContent">
          <a-popover title="入群欢迎语">
            <div class="text-overflow-1 w-full overflow-hidden">{{ record.greetContent }}</div>
            <template #content>
              <div style="word-break: break-word; white-space: pre-line" class="w-70n">{{ record.greetContent }}</div>
            </template>
          </a-popover>
        </template>
        <template v-else>暂未设置</template>
      </template>
    </GiTable>
  </div>
</template>

<script setup>
import { usePagination } from '@/hooks'
import { detailGreetInfo } from '@/apis'
import GiDot from '@/components/GiDot/index.vue'

const props = defineProps(['userWechatId'])

const columns = ref([
  {
    title: '激活群',
    dataIndex: 'groupName',
    width: 200
  },
  {
    title: '入群欢迎语',
    width: 400,
    slotName: 'greetContent'
  },
  {
    title: '开启状态',
    width: 100,
    render: (data) => {
      const status = data.record.isGreet
      const type = status ? 'success' : 'info'
      const text = status ? '开启中' : '未开启'
      return h(GiDot, {
        animation: false,
        type: type,
        text: text
      })
    }
  }
])

const loading = ref(false)
const tableData = ref([])

const { pagination, setTotal } = usePagination(() => getTableData(), { defaultPageSize: 10 })

const listParams = reactive({
  page: 1,
  userWechatId: undefined,
  isOpen: ''
})

// 获取列表数据
const getTableData = async () => {
  try {
    loading.value = true
    listParams.page = pagination.current
    listParams.userWechatId = props.userWechatId
    const res = await detailGreetInfo(listParams)
    tableData.value = res.data.list
    setTotal(res?.data?.meta?.pagination?.total || 0)
  } finally {
    loading.value = false
  }
}

// 表格页码改变
const changeCurrent = (e) => {
  pagination.current = listParams.page = e
  getTableData()
}

watch(
  () => props.userWechatId,
  () => {
    changeCurrent(1)
  },
  { deep: true, immediate: true }
)

//升降序发生改变
const onSorterChange = (dataIndex, direction) => {
  listParams.orderBy = direction ? (direction === 'ascend' ? 'asc' : 'desc') : undefined
  listParams.sort = listParams.orderBy ? dataIndex : undefined
  changeCurrent(1)
}

// 列表重置搜索
const onResetSearch = () => {
  listParams.title = listParams.status = undefined
  changeCurrent(1)
}
</script>

<style lang="scss" scoped>
.table-page {
  height: 100%;
  overflow: hidden;
  background: var(--color-bg-1);
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  .form {
    :deep(.arco-form-item) {
      margin-bottom: 10px;
    }
  }
}
</style>
